/**
 * 
 */
package info.niwota.webshot;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author qiangli
 *
 */
public class RestrictedActivity extends WebSnapshotActivity {
	public static final String ACTION_POWERUP_QUIT = "info.niwota.intent.ACTION_POWERUP_QUIT";

	private static final String TAG = "RestrictedActivity";

	private static final String HOME_URI = "file:///android_asset/index2.html";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		//getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		setSecureFullscreen();
		
		super.onCreate(savedInstanceState);
	}


	@Override
	protected void init(Bundle b) {
		super.init(b);
		//
		backQuit = true;
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean b = super.onKeyDown(keyCode, event);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onKeyDown" + keyCode + " handled: " + b);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return b;
		}
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			return b;
		}
		
		return true;
	}

	
	@Override
	protected void doQuit() {
		super.doQuit();
		
		Intent i = new Intent(ACTION_POWERUP_QUIT);
		sendOrderedBroadcast(i, null);
	}

	@Override
	protected void initRes(Bundle savedInstanceState) {
		super.initRes(savedInstanceState);
		
		hideShare();
	}

	private void hideShare() {
		btnAccept.setVisibility(View.GONE);
		btnAccept.setEnabled(false);
		
		btnShare.setVisibility(View.GONE);
		btnShare.setEnabled(false);
		
		btnCrop.setVisibility(View.GONE);
		btnCrop.setEnabled(false);
	}

	@Override
	protected void showSnapshot() {
		super.showSnapshot();
		
		hideShare();
	}

	@Override
	protected void doAccept() {
	}

	@Override
	protected void doShare() {
	}

	@Override
	protected void doListBookmark(String url) {
	}

	@Override
	protected void doManageBookmarks() {
	}

	@Override
	protected void doViewArchive() {
	}

	@Override
	protected void doSettings() {
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		bookmarkMenuItem.setEnabled(true); //
		quitMenuItem.setEnabled(true);
		
		browseMenuItem.setEnabled(false);
		settingsMenuItem.setEnabled(false);
		capturedMenuItem.setEnabled(false);
		pastaMenuItem.setEnabled(false);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void doPasta() {
		//
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult requestCode: " + requestCode
					+ " resultCode: " + resultCode + " data: " + data);
		}
	}


	@Override
	protected void doPostCrop(int resultCode, Intent data) {
		//super.doPostCrop(resultCode, data);
	}


	@Override
	protected void doCrop() {
		//super.doCrop();
	}

	@Override
	protected void goHome() {
		webview.clearHistory();

		showOrHideNaviBar();
		//
		load(HOME_URI);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_GOTO:
			return new GoDialog(this);
		}
		return super.onCreateDialog(id);
	}
	
	@Override
	protected void checkAndSetScreen() {
		final Window w = getWindow();
		w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		w.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
	}

	private void setSecureFullscreen() {
		final Window w = getWindow();
		w.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		w.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
	}
	
	final private class GoDialog extends Dialog {
		public GoDialog(Context ctx) {
			super(ctx, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
			
			setContentView(R.layout.go);
			//
			final EditText txtUrl = (EditText) findViewById(R.id.goto_url);
			final Button btnGo = (Button) findViewById(R.id.goto_button);
			View.OnClickListener l = new View.OnClickListener() {
				public void onClick(View v) {
					String s = txtUrl.getText().toString();
					dismiss();
					doGoto(s);
				}
			};
			btnGo.setOnClickListener(l);
		}

		@Override
		public void cancel() {
			showOrHideCaptureButton();
			super.cancel();
		}
	}

}
